{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "77bb0e64",
   "metadata": {},
   "source": [
    "# `anywidget` 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ebee4ce3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "fb5a9fc21cd74957a5ad10b210aa4708",
       "version_major": 2,
       "version_minor": 1
      },
      "text/plain": [
       "Counter()"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预加载 anywidget\n",
    "import anywidget\n",
    "import traitlets\n",
    "class Counter(anywidget.AnyWidget):\n",
    "    _esm = \"\"\"\n",
    "    import * as React from \"https://esm.sh/react@18\";\n",
    "    import { createRender, useModelState } from \"https://esm.sh/@anywidget/react?deps=react@18,react-dom@18\";\n",
    "    function Counter() {\n",
    "      let [value, setValue] = useModelState(\"value\");\n",
    "      return React.createElement(\n",
    "        \"button\",\n",
    "        { onClick: () => setValue(value + 1) },\n",
    "        `count is ${value}`\n",
    "      );\n",
    "    }\n",
    "    export let render = createRender(Counter);\n",
    "    \"\"\"\n",
    "    value = traitlets.Int(0).tag(sync=True)\n",
    "\n",
    "Counter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4e1d99fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7441f1e7c5734285a8a0ec3fd84498e1",
       "version_major": 2,
       "version_minor": 1
      },
      "text/plain": [
       "ExampleWidget()"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import anywidget\n",
    "import traitlets\n",
    "\n",
    "class ExampleWidget(anywidget.AnyWidget):\n",
    "    # anywidget, required #\n",
    "    _esm = \"\"\"\n",
    "    function render({ model, el }) {\n",
    "        el.classList.add(\"custom-widget\");\n",
    "        function value_changed() {\n",
    "            el.textContent = model.get(\"value\");\n",
    "        }\n",
    "        value_changed();\n",
    "        model.on(\"change:value\", value_changed);\n",
    "    }\n",
    "    export default { render };\n",
    "    \"\"\"\n",
    "    # anywidget, optional #\n",
    "    _css = \"\"\"\n",
    "    .custom-widget {\n",
    "        background-color: lightseagreen;\n",
    "        padding: 0px 2px;\n",
    "    }\n",
    "    \"\"\"\n",
    "    # custom state for the widget #\n",
    "    value = traitlets.Unicode(\"Hello World\").tag(sync=True)\n",
    "\n",
    "ExampleWidget()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py313",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
